package com.karry.yl.web.services;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.baseinfo.Building;
import com.karry.yl.domain.baseinfo.Room;
import com.karry.yl.domain.services.RoundsDdeviant;
import com.karry.yl.service.baseinfo.BuildingService;
import com.karry.yl.service.baseinfo.RoomService;
import com.karry.yl.service.services.RoundsDeviantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/roundsDeviant")
public class RoundsDeviantController extends SuperController {

    @Autowired
    private RoundsDeviantService roundsDeviantService;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private RoomService roomService;
    @RequestMapping
    public String listUI(){
        return "services/roundsdeviant/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<RoundsDdeviant> itemsPage = roundsDeviantService.selectPage(new Page<RoundsDdeviant>(page, limit),new EntityWrapper<RoundsDdeviant>().eq("orgId",currentUser.getOrgId()));
        return new LayTableResponse(itemsPage.getRecords(),itemsPage.getTotal());
    }

    @RequestMapping("/add")
    public String addUI(ModelMap map){
        List<Building> buildings = buildingService.selectList(new EntityWrapper<Building>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("status",Building.STATUS_TY));
        map.put("buildings",buildings);
        return "services/roundsdeviant/add";
    }

    @RequestMapping("/save")
    public String save(RoundsDdeviant roundsDdeviant){
        roundsDdeviant.setOrgId(currentUser.getOrgId());
        roundsDdeviant.setRoundsStatus("1");
        roundsDdeviant.setCfUser(currentUser.getUserName());
        roundsDdeviant.setStatus("1");
        roundsDeviantService.insert(roundsDdeviant);
        return "redirect:/roundsDeviant";
    }

    @RequestMapping("edit")
    public String editUI(Long id,Long buildingId,ModelMap map) {
        RoundsDdeviant roundsDdeviant = roundsDeviantService.selectById(id);
        map.put("roundsDdeviant", roundsDdeviant);
        List<Building> buildings = buildingService.selectList(new EntityWrapper<Building>().eq("orgId", currentUser.getOrgId()).eq("status",Building.STATUS_TY));
        map.put("buildings",buildings);
        Room room=new Room();
        room.setOrgId(currentUser.getOrgId());
        room.setBuildingId(buildingId);
        List<Integer> floors = roomService.selectRooms(room);
        map.put("floors",floors);
        List<Room> rooms = roomService.selectList(new EntityWrapper<Room>().eq("orgId", currentUser.getOrgId()).eq("buildingId", buildingId).eq("floor",roundsDdeviant.getFloor()));
        map.put("rooms",rooms);
        return "services/roundsdeviant/edit";
    }

    @RequestMapping("/update")
    public String update(RoundsDdeviant roundsDdeviant){
        roundsDdeviant.setOrgId(currentUser.getOrgId());
        roundsDeviantService.updateById(roundsDdeviant);
        return "redirect:/roundsDeviant";
    }
    /**
     * 删除
     *
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids) {
        roundsDeviantService.deleteBatchIds(ids);
        return "redirect:/roundsDeviant";
    }
}
